﻿/*
  Copyright (c) 2008, Adobe Systems Incorporated
  All rights reserved.

  Redistribution and use in source and binary forms, with or without 
  modification, are permitted provided that the following conditions are
  met:

  * Redistributions of source code must retain the above copyright notice, 
    this list of conditions and the following disclaimer.
  
  * Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions and the following disclaimer in the 
    documentation and/or other materials provided with the distribution.
  
  * Neither the name of Adobe Systems Incorporated nor the names of its 
    contributors may be used to endorse or promote products derived from 
    this software without specific prior written permission.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package tools.utils {
	import flash.utils.ByteArray;

	/**
	* 	
	*	Note that all APIs assume that they are working with well formed arrays.
	*	i.e. they will only manipulate indexed values.  
	* 
	* 	@langversion ActionScript 3.0
	*	@playerversion Flash 9.0
	*	@tiptext
	*/		
	public class ArrayUtil
	{
		/*
		 * 在数组中随机获取其n个元素的子数组
		 */
		public static function getChildRandomArr(targetArray:Array,num:int):Array
		{
			var result : Array = [];
			var indexArr:Array = [];
			if (num <= targetArray.length) {
				while (indexArr.length < num) {
					var random : int = Rnd.integer(targetArray.length);
					if (indexArr.indexOf(random) < 0) {
						indexArr.push(random);
					}
				}
				for (var i:int = 0; i < indexArr.length; i++) 
				{
					var item:* = targetArray[indexArr[i]];
					result.push(item);
				}
			} else {
				result = null;
			}
			return result;
		}
		
		/**
		 *   随机排列一个数组中的值
		 */
		public static function randomOrder (targetArray:Array):Array
		{
			var arrayLength:int = targetArray.length
			//先创建一个正常顺序的数组
			var tempArray1:Array = new Array();
			for (var i:int = 0; i < arrayLength; i ++)
			{
				tempArray1 [i] = i
			}
			//再根据上一个数组创建一个随机乱序的数组
			var tempArray2:Array = new Array();

			for (i = 0; i < arrayLength; i ++)
			{
				//从正常顺序数组中随机抽出元素
				tempArray2 [i] = tempArray1.splice (Math.floor (Math.random () * tempArray1.length) , 1)
			}
			//最后创建一个临时数组存储 根据上一个乱序的数组从targetArray中取得数据
			var tempArray3:Array = new Array();

			for (i = 0; i < arrayLength; i ++)
			{
				tempArray3 [i] = targetArray [tempArray2 [i]]
			}
			//
			//返回最后得出的数组
			return tempArray3
		}
				
		/**
		*	检测数组中是否包含这个值.	
		* 
		* 	@param arr The array that will be checked for the specified value.
		*
		*	@param value The object which will be searched for within the array
		* 
		* 	@return True if the array contains the value, False if it does not.
		*
		* 	@langversion ActionScript 3.0
		*	@playerversion Flash 9.0
		*	@tiptext
		*/			
		public static function arrayContainsValue(arr:Array, value:Object):Boolean
		{
			return (arr.indexOf(value) != -1);
		}	
		
		/**
		*	删除数组中指定的值,
		* 
		* 	@param arr The array from which the value will be removed
		*
		*	@param value The object that will be removed from the array.
		*
		* 	@langversion ActionScript 3.0
		*	@playerversion Flash 9.0
		*	@tiptext
		*/		
		public static function removeValueFromArray(arr:Array, value:Object):void
		{
			var len:uint = arr.length;
			
			for(var i:Number = len - 1; i > -1; i--)
			{
				if(arr[i] === value)
				{
					arr.splice(i, 1);
				}
			}					
		}

		/**
		*	在指定数组中创建一个新数组，该数组的每个值都是独一无二的(即无重复值)。
		*   用来清除原数组中重复的复制
		* 
		* 	@param arr The array which contains the values that will be used to
		*	create the new array that contains no duplicate values.
		*
		*	@return A new array which only contains unique items from the specified
		*	array.
		*
		* 	@langversion ActionScript 3.0
		*	@playerversion Flash 9.0
		*	@tiptext
		*/	
		public static function createUniqueCopy(a:Array):Array
		{
			var newArray:Array = new Array();
			
			var len:Number = a.length;
			var item:Object;
			
			for (var i:uint = 0; i < len; ++i)
			{
				item = a[i];
				
				if(ArrayUtil.arrayContainsValue(newArray, item))
				{
					continue;
				}
				
				newArray.push(item);
			}
			
			return newArray;
		}
		
		/**
		*	建立一个新引用，深度复制数组
		* 
		* 	@param arr The array that will be copies
		*
		*	@return A new array which contains the same items as the array passed
		*	in.
		*
		* 	@langversion ActionScript 3.0
		*	@playerversion Flash 9.0
		*	@tiptext
		*/			
		public static function copyArray(arr:Array):Array
		{	
			var temp:Array = [];
			var n:uint = arr.length;
			for (var i:int = 0; i < n; i++) 
			{
				if (arr[i] is Array) {
					temp.push(copyArray(arr[i]));
				}else {
					temp.push(arr[i]);
				}
			}
			return temp;
		}
		
		/**
		 * 深度复制
		* @param source 源
		*/
		public static function clone(source:Object):*{
			var copier : ByteArray = new ByteArray();
			copier.writeObject(source);
			copier.position = 0;
			return (copier.readObject());
		}
		
		/**
		*	匹配两个数组		* 
		* 	@param arr1 The first array that will be compared to the second.
		*
		* 	@param arr2 The second array that will be compared to the first.
		*
		*	@return True if the arrays contains the same values at the same indexes.
			False if they do not.
		*
		* 	@langversion ActionScript 3.0
		*	@playerversion Flash 9.0
		*	@tiptext
		*/		
		public static function arraysAreEqual(arr1:Array, arr2:Array):Boolean
		{
			if(arr1.length != arr2.length)
			{
				return false;
			}
			
			var len:Number = arr1.length;
			
			for(var i:Number = 0; i < len; i++)
			{
				if(arr1[i] !== arr2[i])
				{
					return false;
				}
			}
			
			return true;
		}
		
		public static function randomNumber(num:int,max:int):Array
		{
			var arr : Array = [];
			while (arr.length < num) {
				var random : int = Rnd.integer(max);
				if (arr.indexOf(random) < 0) {
					arr.push(random);
				}
			}
			return arr;
		}
	}
}
